package com.base.vistter.iframe.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static final String DATE_SPLIT = "-";

    public static final String TIME_SPLIT = ":";

    public static DateFormat yearFormat = new SimpleDateFormat("yyyy");

    public static DateFormat monthFormat = new SimpleDateFormat("yyyy-MM");

    /**
     * 日期格式
     */
    public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 分钟格式
     */
    public static DateFormat minuteFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    /**
     * 天格式
     */
    public static DateFormat dayFormat = new SimpleDateFormat("MM-dd");

    /**
     * 获取周几
     *
     * @param week
     * @return
     */
    public static String getWeek(int week) {
        String weekStr = null;
        switch (week) {
            case Calendar.SUNDAY:
                weekStr = "周日";
                break;
            case Calendar.MONDAY:
                weekStr = "周一";
                break;
            case Calendar.TUESDAY:
                weekStr = "周二";
                break;
            case Calendar.WEDNESDAY:
                weekStr = "周三";
                break;
            case Calendar.THURSDAY:
                weekStr = "周四";
                break;
            case Calendar.FRIDAY:
                weekStr = "周五";
                break;
            case Calendar.SATURDAY:
                weekStr = "周六";
                break;
            default:
                break;
        }
        return weekStr;
    }

    public static boolean isCurrentYear(Date date) {
        return yearFormat.format(date).equals(yearFormat.format(new Date()));
    }

    public static boolean isCurrentMonth(Date date) {
        return monthFormat.format(date).equals(monthFormat.format(new Date()));
    }

    /**
     * 是否同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        return dateFormat.format(date1).equals(dateFormat.format(date2));
    }

    /**
     * 判断今天
     */
    public static boolean isToday(Date time) {
        return isSameDay(time, new Date());
    }

    /**
     * 是否同一分钟
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameMinute(Date date1, Date date2) {
        return minuteFormat.format(date1).equals(minuteFormat.format(date2));
    }

    /**
     * 是否周末
     *
     * @param calendar
     * @return
     */
    public static boolean isWeekend(Calendar calendar) {
        return calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY;
    }

    /**
     * 字符串转换成时间
     *
     * @param dateText
     * @return
     */
    public static Date parseDate(String dateText, DateFormat dateFormat) throws ParseException {
        return dateFormat.parse(dateText);
    }

    /**
     * 日期转换成字符串
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String parseString(Date date, DateFormat dateFormat) {
        return dateFormat.format(date);
    }

    /**
     * 判断时间在时间区域内容
     *
     * @param time      判断的时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static boolean isBetween(Date time, Date startTime, Date endTime) {
        return (startTime.before(time) && time.before(endTime));
    }

    /**
     * 判断开始时间是否在范围内，
     *
     * @return
     */
    public static boolean isStartBetween(Calendar calendar, Date startTime, Date endTime) {
        return isSameMinute(calendar.getTime(), startTime) || (startTime.before(calendar.getTime()) && calendar.getTime().before(endTime));
    }

    /**
     * 判断结束时间是否在范围内，
     *
     * @return
     */
    public static boolean isEndBetween(Calendar calendar, Date startTime, Date endTime) {
        return isSameMinute(calendar.getTime(), endTime) || (startTime.before(calendar.getTime()) && calendar.getTime().before(endTime));
    }

    //获取今日开始时间
    public static Date getTodayStartTime() {
        Calendar calendar = Calendar.getInstance();
        return getDateStartTime(calendar.getTime());
    }

    //获取今日结束时间
    public static Date getTodayEndTime() {
        Calendar calendar = Calendar.getInstance();
        return getDateEndTime(calendar.getTime());
    }

    public static Date getYearStartTime(Date time){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MONTH ,0);
        calendar.set(Calendar.DAY_OF_MONTH ,1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getYearEndTime(Date time){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MONTH ,11);
        calendar.set(Calendar.DAY_OF_MONTH ,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getMonthStartTime(Date time){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.DAY_OF_MONTH ,1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getMonthEndTime(Date time){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.DAY_OF_MONTH ,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getDateStartTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getDateEndTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }
}
